<!-- 公共项 -->

<template>
  <div class="flex" :id="`form-item-${obj.no}`">
    <ShowSlot v-if="obj.no" noLabel flex0>
      <div :class="{ 'DetailQuestionnaire-MyStatistic-required': obj.required === 1 }">{{ obj.no }}</div>
    </ShowSlot>

    <div v-if="obj.title" class="flex1">
      <div class="DetailQuestionnaire-MyStatistic-title flex">
        <div class="flex1">
          <span class="ellips">{{ obj.title }}</span>
        </div>
        <div class="flex0">回答数：{{ count }}</div>
        <ChartBar v-bind="chartProps" />
        <BtnText title="查看本题答卷" @click="statistic_openShow({ no: obj.no })" />
      </div>

      <div :class="`DetailQuestionnaire-MyStatistic-${obj.type}`">
        <slot />
      </div>
    </div>
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import props from './props'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'OurItem',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb('statistic', 'statistic_'), CORE.mixWeb('answer', 'answer_')],

  /**
   * 属性注册 (抽取以便查阅)
   */
  props,

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 回答数
     */
    count() {
      const { obj: { id }, answer_list } = this
      const matched = answer_list.filter((answer) => {
        return answer.dtos.some((dto) => {
          return (dto.questionId === id || dto.parentQuestionId === id) && (dto.optionId || dto.value)
        })
      })
      return matched.length
    },

    /**
     * 图表属性
     */
    chartProps() {
      const { count, answer_list } = this
      const length = answer_list.length || 1
      return {
        dataList: [+(count * 100 / length).toFixed(2)],
        [count === length ? 'green' : 'golden']: true,
        percent: true,
        noTip: true,
      }
    },
  },
}
</script>
